package cn.dankal.flyaudio.presenter;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import api.ShopUserServiceFactory;
import cn.dankal.basiclib.base.BasePresenter;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.ProductEntity;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.contract.AddressManagerContract;
import cn.dankal.flyaudio.contract.StoreSearchContract;

public class StoreSearchPresenter implements StoreSearchContract.Presenter {

    public static final String ORDER_BY_PRICE = "showPrice";

    public static final String ORDER_BY_EVALUATE = "goodCommentCount";

    public static final String ORDER_BY_SALES = "saleCount";

    private StoreSearchContract.View mView;

    private int page = 1;
    private int size = 10;


    @Override
    public void attachView(@NonNull StoreSearchContract.View view) {
        mView = view;
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public void doSearch(String keyword, boolean isRefresh) {
        if(isRefresh){
            page = 1;
        }
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("keyword", keyword);
        queryMap.put("page", page);
        queryMap.put("size", size);
        ShopUserServiceFactory.searchShop(queryMap)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<ProductEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<ProductEntity>> productEntityBaseResponse) {
                        mView.showProductList(productEntityBaseResponse.getData().getList(), isRefresh);
                    }
                });
    }

    @Override
    public void doSearch(String keyword, boolean isRefresh, String sort, boolean asc) {
        if(isRefresh){
            page = 1;
        }
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("orderBy", sort);
        queryMap.put("keyword", keyword);
        queryMap.put("asc", asc);
        queryMap.put("page", page);
        queryMap.put("size", size);
        ShopUserServiceFactory.searchShop(queryMap)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<ProductEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<ProductEntity>> productEntityBaseResponse) {
                        mView.showProductList(productEntityBaseResponse.getData().getList(), isRefresh);
                    }
                });
    }
}
